﻿Public Class FormCheck
    Public usr As String
    Dim objGt As New clsGt
    Dim objGtResultDetails As New clsGtResultDetails
    Dim objGtNgReasonDetails As New clsGtNgReasonDetails
    Dim objGtProcedure As New clsGtProcedure
    Dim objGtProcedureDetails As New clsGtProcedureDetails
    Dim txt As TextBox
    Private Sub FormCheck_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        Dim startDate As String = Date.Now.AddDays(-1).ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub
    Private Sub btnSearce_Click(sender As Object, e As EventArgs) Handles btnSearce.Click
        setDgvData()
    End Sub

    Public Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dtGt As DataTable = objGt.getGtList(txtStartDate.Text.Trim() + " 00:00:00.000", txtEndDate.Text.Trim() + " 23:59:59.999")
            dtGt.Columns.Add("oper_type_name")
            If dtGt.Rows.Count > 0 Then
                For Each row As DataRow In dtGt.Rows
                    If row("oper_type") = "0" Then
                        row("oper_type_name") = "骨架板"
                    Else
                        row("oper_type_name") = "法兰板"
                    End If
                Next
            End If
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dtGt
        Else
            MsgBox("请输入开始时间和结束时间")
        End If
    End Sub

    Public Sub setDgvOperData()
        Dim dtProcedure As DataTable = objGtProcedure.selectGtProcedure(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
        dtProcedure.Columns.Add("procedure_no_name")
        If dtProcedure.Rows.Count > 0 Then
            For Each row As DataRow In dtProcedure.Rows
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("0") Then
                    If row("procedure_no") = "0" Then
                        row("procedure_no_name") = "校平"
                    End If
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "六工位"
                    End If
                End If
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("1") Then
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "三工位"
                    End If
                End If
            Next
        End If
        DataGridView3.AutoGenerateColumns = False
        DataGridView3.DataSource = dtProcedure
        Dim dtDetails1 As DataTable = objGtProcedureDetails.getGtProcedureDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
        dtDetails1.Columns.Add("procedure_no_name")
        If dtDetails1.Rows.Count > 0 Then
            For Each row As DataRow In dtDetails1.Rows
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("0") Then
                    If row("procedure_no") = "0" Then
                        row("procedure_no_name") = "校平"
                    End If
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "六工位"
                    End If
                End If
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("1") Then
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "三工位"
                    End If
                End If
            Next
        End If
        dgvOper.AutoGenerateColumns = False
        dgvOper.DataSource = dtDetails1
    End Sub

    Public Sub SetDgvResultData0()
        Dim dtResult0 As DataTable = objGtResultDetails.getGtResultDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "0")
        DataGridView1.AutoGenerateColumns = False
        DataGridView1.DataSource = dtResult0
    End Sub

    Public Sub SetDgvResultData1()
        Dim dtResult1 As DataTable = objGtResultDetails.getGtOperResultDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "0")
        dtResult1.Columns.Add("procedure_no_name")
        If dtResult1.Rows.Count > 0 Then
            For Each row As DataRow In dtResult1.Rows
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("0") Then
                    If row("procedure_no") = "0" Then
                        row("procedure_no_name") = "校平"
                    End If
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "六工位"
                    End If
                End If
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("1") Then
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "三工位"
                    End If
                End If
            Next
        End If
        DataGridView2.AutoGenerateColumns = False
        DataGridView2.DataSource = dtResult1
    End Sub

    Public Sub setDgvNgData()
        Dim dtNg As DataTable = objGtNgReasonDetails.getGtNgDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "0")
        dgvNg.AutoGenerateColumns = False
        dgvNg.DataSource = dtNg
    End Sub

    Public Sub setDgvNgOperData()
        Dim dtNgDetails As DataTable = objGtNgReasonDetails.getGtOperNgDetailsList(dgv.SelectedRows(0).Cells("sys_id").Value.ToString(), "0")
        dtNgDetails.Columns.Add("procedure_no_name")
        If dtNgDetails.Rows.Count > 0 Then
            For Each row As DataRow In dtNgDetails.Rows
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("0") Then
                    If row("procedure_no") = "00" Then
                        row("procedure_no_name") = "领料"
                    End If
                    If row("procedure_no") = "0" Then
                        row("procedure_no_name") = "校平"
                    End If
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "六工位"
                    End If
                End If
                If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("1") Then
                    If row("procedure_no") = "00" Then
                        row("procedure_no_name") = "领料"
                    End If
                    If row("procedure_no") = "1" Then
                        row("procedure_no_name") = "喷砂清洗"
                    End If
                    If row("procedure_no") = "2" Then
                        row("procedure_no_name") = "三工位"
                    End If
                End If
            Next
        End If
        dgvNgOper.AutoGenerateColumns = False
        dgvNgOper.DataSource = dtNgDetails
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        If dgv.SelectedRows.Count > 0 Then
            If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("0") Then
                btnFactoryOutsource.Enabled = True
            End If
            If dgv.SelectedRows(0).Cells("oper_type").Value.ToString().Equals("1") Then
                btnFactoryOutsource.Enabled = False
            End If
            SetDgvResultData0()
            setDgvNgData()
            setDgvOperData()
            SetDgvResultData1()
            setDgvNgOperData()
        End If
    End Sub

    Private Sub btnNg_Click(sender As Object, e As EventArgs) Handles btnNg.Click
        If dgvOper.SelectedRows.Count > 0 Then
            If DataGridView2.SelectedRows.Count > 0 Then
                Dim formNgOper As New FormNgOper()
                formNgOper.usr = usr
                formNgOper.source = "check"
                formNgOper.ty = dgv.SelectedRows(0).Cells("oper_type").Value.ToString()
                formNgOper.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
                formNgOper.current_procedure_No = Integer.Parse(DataGridView2.SelectedRows(0).Cells("C_procedure_no").Value.ToString())
                formNgOper.ng_num_all = DataGridView2.SelectedRows(0).Cells("Co_ng_num").Value.ToString()
                Dim oper_num As String = "0"
                For index = 0 To DataGridView3.Rows.Count - 1
                    If DataGridView3.Rows(index).Cells("Co_procedure_no").Value.ToString().Equals(DataGridView2.SelectedRows(0).Cells("C_procedure_no").Value.ToString()) Then
                        oper_num = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
                    End If
                Next
                formNgOper.oper_num_all = oper_num
                formNgOper.ShowDialog(Me)
            End If
        End If
    End Sub

    Private Sub btnFactoryOutsource_Click(sender As Object, e As EventArgs) Handles btnFactoryOutsource.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim sys_id As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            Dim assist_marker_jnj As String = dgv.SelectedRows(0).Cells("Col_assist_marker_jnj").Value.ToString()
            Dim formFactoryOutsourceSave As New FormFactoryOutsourceSave()
            formFactoryOutsourceSave.source = "check"
            formFactoryOutsourceSave.sys_id = sys_id
            formFactoryOutsourceSave.assist_marker_jnj = assist_marker_jnj
            formFactoryOutsourceSave.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnNgPw_Click(sender As Object, e As EventArgs) Handles btnNgPw.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim formNgPw As New FormNgPw()
            formNgPw.usr = usr
            formNgPw.source = "check"
            formNgPw.ty = dgv.SelectedRows(0).Cells("oper_type").Value.ToString()
            formNgPw.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            formNgPw.picking_num = dgv.SelectedRows(0).Cells("picking_num").Value.ToString()
            If DataGridView1.Rows.Count > 0 Then
                formNgPw.ng_pw_num_all = DataGridView1.SelectedRows(0).Cells("C_ng_num").Value.ToString()
            Else
                formNgPw.ng_pw_num_all = "0"
            End If
            If DataGridView3.Rows.Count > 0 Then
                formNgPw.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
            Else
                formNgPw.oper_num_all = "0"
            End If
            formNgPw.ShowDialog(Me)
        End If
    End Sub

    Private Sub btnValidityPw_Click(sender As Object, e As EventArgs) Handles btnValidityPw.Click
        If dgvNg.SelectedRows.Count > 0 Then
            If dgvNg.SelectedRows(0).Cells("Col_validity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvNg.SelectedRows(0).Cells("Col_sys_id").Value.ToString()
                    Dim ng_num As String = dgvNg.SelectedRows(0).Cells("Col_ng_num").Value.ToString()
                    Dim b_gt_sys_id As String = dgvNg.SelectedRows(0).Cells("Co_b_gt_sys_id").Value.ToString()
                    Dim procedure_no As String = dgvNg.SelectedRows(0).Cells("Col_procedure_no").Value.ToString()
                    Dim rtn As Integer = objGtNgReasonDetails.updateValidity(sys_id, ng_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        SetDgvResultData0()
                        setDgvNgData()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnOper_Click(sender As Object, e As EventArgs) Handles btnOper.Click
        If dgv.SelectedRows.Count > 0 Then
            If DataGridView3.SelectedRows.Count > 0 Then
                If DataGridView3.SelectedRows(0).Cells("Co_procedure_no").Value.ToString().Equals("1") Then
                    Dim formOper0 As New FormOper0()
                    formOper0.usr = usr
                    formOper0.source = "check"
                    formOper0.ty = dgv.SelectedRows(0).Cells("oper_type").Value.ToString()
                    formOper0.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
                    If DataGridView1.Rows.Count > 0 Then
                        formOper0.pw_ok_num_all = DataGridView1.SelectedRows(0).Cells("Col_ok_num").Value.ToString()
                    Else
                        formOper0.pw_ok_num_all = "0"
                    End If
                    If DataGridView3.Rows.Count > 0 Then
                        formOper0.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
                    Else
                        formOper0.oper_num_all = "0"
                    End If
                    formOper0.ShowDialog(Me)
                Else
                    Dim formOper As New FormOper()
                    formOper.usr = usr
                    formOper.source = "check"
                    formOper.b_gt_sys_id = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
                    Dim ok_num_all As String = "0"
                    For index = 0 To DataGridView2.Rows.Count - 1
                        If Integer.Parse(DataGridView3.SelectedRows(0).Cells("Co_procedure_no").Value.ToString()) = Integer.Parse(DataGridView2.Rows(index).Cells("C_procedure_no").Value.ToString()) + 1 Then
                            ok_num_all = DataGridView2.Rows(index).Cells("C_ok_num").Value.ToString()
                        End If
                    Next
                    formOper.ok_num_all = ok_num_all
                    formOper.oper_num_all = DataGridView3.SelectedRows(0).Cells("Col_oper_num").Value.ToString()
                    formOper.current_procedure_No = Integer.Parse(DataGridView3.SelectedRows(0).Cells("Co_procedure_no").Value.ToString())
                    formOper.ShowDialog(Me)
                End If
            End If

        End If
    End Sub

    Private Sub btnValidityOper_Click(sender As Object, e As EventArgs) Handles btnValidityOper.Click
        If dgvOper.SelectedRows.Count > 0 Then
            If dgvOper.SelectedRows(0).Cells("Colvalidity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvOper.SelectedRows(0).Cells("Colsys_id").Value.ToString()
                    Dim procedure_no As String = dgvOper.SelectedRows(0).Cells("Colprocedure_no").Value.ToString()
                    Dim b_gt_sys_id As String = dgvOper.SelectedRows(0).Cells("Colb_gt_sys_id").Value.ToString()
                    Dim oper_num As String = dgvOper.SelectedRows(0).Cells("Coloper_num").Value.ToString()
                    Dim rtn As Integer = objGtProcedureDetails.updateValidity(sys_id, oper_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        setDgvOperData()
                        SetDgvResultData1()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub btnValidityNg_Click(sender As Object, e As EventArgs) Handles btnValidityNg.Click
        If dgvNgOper.SelectedRows.Count > 0 Then
            If dgvNgOper.SelectedRows(0).Cells("Colu_validity").Value.ToString().Equals("1") Then
                If MsgBox("确认要进行作废吗？", vbYesNo, "提示") = vbYes Then
                    Dim sys_id As String = dgvNgOper.SelectedRows(0).Cells("Colu_sys_id").Value.ToString()
                    Dim procedure_no As String = dgvNgOper.SelectedRows(0).Cells("Colu_procedure_no").Value.ToString()
                    Dim b_gt_sys_id As String = dgvNgOper.SelectedRows(0).Cells("Colu_b_gt_sys_id").Value.ToString()
                    Dim ng_num As String = dgvNgOper.SelectedRows(0).Cells("Colu_ng_num").Value.ToString()
                    Dim rtn As Integer = objGtNgReasonDetails.updateValidity(sys_id, ng_num, b_gt_sys_id, procedure_no, usr)
                    If rtn <> -2 Then
                        MsgBox("作废成功")
                        SetDgvResultData1()
                        setDgvNgOperData()
                    Else
                        MsgBox("作废失败")
                    End If
                End If
            Else
                MsgBox("该记录已作废")
            End If
        End If
    End Sub

    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

End Class